package com.heima.schedule.controller;

import com.heima.schedule.dto.Task;
import com.heima.schedule.service.TaskService;
import com.heima.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/task")
@Slf4j
public class ScheduleController {

    @Autowired
    private TaskService taskService;

    /**
     * 添加任务
     *
     * @param task
     * @return
     */
    @PostMapping("/add")
    public ResponseResult saveTask(@RequestBody Task task) {
        log.info("ScheduleController--->添加任务执行");
        return ResponseResult.okResult(taskService.saveTask(task));
    }


    /**
     * 取消任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/cancel/{taskId}")
    public ResponseResult cancelTask(@PathVariable("taskId") Long taskId) {
        log.info("ScheduleController--->取消任务执行");
        return ResponseResult.okResult(taskService.cancelTask(taskId));
    }

    /**
     * 根据任务类型和优先级拉取任务
     *
     * @param type
     * @param priority
     * @return
     */
    @GetMapping("/pull/{type}/{priority}")
    public ResponseResult pullTask(@PathVariable("type") int type, @PathVariable("priority") int priority) {
        log.info("ScheduleController--->拉取任务执行");
        return ResponseResult.okResult(taskService.pullTask(type, priority));
    }

}
